
import UIKit

extension FixedWidthInteger {
    
    var littleEndianData: Data {
        withUnsafeBytes(of: self.littleEndian) { Data($0) }
    }

    func toData() -> Data {
        withUnsafeBytes(of: self.littleEndian) { Data($0) }
    }
    
    static func fromData(_ data: Data, at offset: Int = 0) -> Self? {
        let size = MemoryLayout<Self>.size
        guard data.count >= offset + size else { return nil }

        return data.subdata(in: offset..<offset + size).withUnsafeBytes {
            $0.load(as: Self.self).littleEndian
        }
    }
 
    var scaled: CGFloat {
        let value = CGFloat(Int(self)) * UIScreen.main.bounds.width / 390.0
        return value.rounded()
     }

}

extension BinaryFloatingPoint {
    var scaled: CGFloat {
        return CGFloat(self) * UIScreen.main.bounds.width / 390.0
    }
}
